// router  js
const express=require('express');
const upload=express.Router();
const axios = require("axios");
const qs=require("qs")
const FormData = require('form-data')
const fs = require('fs')
const path = require('path')
const multipart = require('connect-multiparty');
var multipartMiddleware = multipart()
const jwt = require("jsonwebtoken")
var http = require('http')
const FileReader = require('FileReader');
const { Console } = require('console');
upload.use(express.urlencoded({extended:false}))
upload.use(express.json())

//根据学号查询信息
upload.get('/getInfoByStuId',multipartMiddleware,(req,res)=>{
    let {stuId} = req.query
    if (stuId) {
        axios.get(`http://1.117.92.175:8081/user/findByStudentId?studentId=${stuId}`,{
          headers:{
            token:req.session.token?req.session.token:"",
          },
        })
        .then((data)=>{
           res.send({data:data.data})
           console.log(data.data)
        })
        .catch((err)=>{
            res.send({err:err})
        })
        
    }else{
        return res.status(400).render('admin/error',{msg:"参数错误"})
    }
})

//上传简历正面
upload.post('/uploadThis',multipartMiddleware,(req,myRes)=>{
    let {studentId,path} = req.body;
    let {file} = req.files;
    console.log(file)
    
    let formData = new FormData();
    formData.append('file',fs.createReadStream(file.path))
    let H=formData.getHeaders()
    H['token']=req.session.token ? req.session.token : ''
    var http = require('http');

    var request = http.request({
      method: 'post',
      host: '1.117.92.175',
      port: 8081,
      path: `http://1.117.92.175:8081/upLoad?path=${path}&studentId=${studentId}`,
      headers: H,
    },function(res){
        var str='';
        res.on('data',function(buffer){
          str+=buffer;//用字符串拼接
        }
        );
        res.on('end',()=>{
          console.log(str)
          var result = JSON.parse(str);
          //上传之后result就是返回的结果
          console.log(result)
          myRes.send({data:result})
        }); 
      }
    )
    request.on('error', (e) => {
        console.error(`problem with request: ${e.message}`);
      });
      formData.pipe(request);

})


upload.get('/getInfoByid',(req,res)=>{
  let {id} = req.query;
    if(id){
      axios.get(`http://1.117.92.175:8081/admin/findUserById?id=${id}`,{
        headers:{
          token:req.session.token?req.session.token:"",
        },
      })
      .then((data)=>{
        res.send({data:data.data})
      })
      .catch((err)=>{
        console.log(err)
      })
    }else{
      return res.status(400).render('admin/error',{msg:"参数错误"})
    }
})
//根据名字模糊查询
upload.get('/getInfoByName',(req,res)=>{
  let {name} = req.query;
  let urlName = encodeURIComponent(name);
  console.log(name)
  if(name){
    axios.get(`http://1.117.92.175:8081/findByNameLike?name=${urlName}`,{
      headers:{
        token:req.session.token?req.session.token:"",
      },
    })
    .then((data)=>{
      console.log(data.data)
      res.send({data:data.data})
    })
    .catch((err)=>{
      res.send({err:-1})
    })
  }else{
    return res.status(400).render('admin/error',{msg:"参数错误"})
  }
})

upload.get('/changeSelf',(req,res)=>{
  let {id,name,password,username,classId,sex,studentId} = req.query;
  if(id){
    axios.get(`http://1.117.92.175:8081/admin/updateUser?id=${id}&name=${encodeURIComponent(name)}&password=${password}&username=${username}&classId=${encodeURIComponent(classId)}&sex=${encodeURIComponent(sex)}&studentId=${studentId}`,{
      headers:{
        token:req.session.token?req.session.token:"",
      },
    })
    .then((data)=>{
      res.send({data:data.data})
      console.log(data)
    })
    .catch((err)=>{
      res.send({err:-1})
      console.log(err)
    })
  }else{
    return res.status(400).render('admin/error',{msg:"参数错误"})
  }
})

upload.get('/getMyInterview',(req,res)=>{
  let {user} =req.query;
  let name = encodeURIComponent(user);
  if(user){
    axios.get(`http://1.117.92.175:8081/findByUserLike?user=${name}`,{
      headers:{
        token:req.session.token?req.session.token:"",
      },
    })
    .then((data)=>{
      res.send({info:data.data})
    })
    .catch((err)=>{
      console.log(err)
    })
  }else{
    return res.status(400).render('admin/error',{msg:"参数错误"})
  }
})


module.exports=upload;